******************************************************************************** 
* Run Directory File
* Last revision: 1/13/2025
* Gary Gorth, Elizabeth Klee, Chase Ross, Sharon Ross, Alexandros Vardoulakis
*
* Runs all code necessary to clean data, calculate series, run regressions, etc.
*
* Notes: 
* - Included data is pseudodata
* - Python environment packages are provided in the environment.yml
* - Code below is provided in order; python programs must be run separately

******************************************************************************** 

******************************************************************************** 
* Program Setup
******************************************************************************** 
version 17              // Set Version number for backward compatibility
set more off            // Disableapartitioned output
clear all               // Start with a clean slate
set linesize 80         // Line size limit to make output more readable
macro drop _all         // clear all macros
capture log close       // Close existing log files
noisily: di _newline(100) 	// Space out
******************************************************************************** 

******************************************************************************** 
* Set directory by user
******************************************************************************** 
cd
cd "path/to/replication_package"

capture shell rm -r stata_files
mkdir stata_files
sysdir set PERSONAL stata_files

******************************************************************************** 

******************************************************************************** 
* Download utilities
********************************************************************************
ssc install grstyle, replace
ssc install palettes, replace
ssc install colrspace, replace
ssc install estout, replace
ssc install pdslasso, replace
ssc install LABUTIL, replace
ssc install eclplot, replace
ssc install asreg, replace
ssc install ivreg2, replace
ssc install ranktest, replace
ssc install xtscc, replace
ssc install rangestat, replace
net install grc1leg, from(http://www.stata.com/users/vwiggins)

******************************************************************************** 
* Run Build Code
******************************************************************************** 

// Note: API is no longer functional as of 11/11/2022
//       - API endpoint: https://web.archive.org/web/20210407035636/https://ftx.com/api/spot_margin/history
//       - API documentation: https://web.archive.org/web/20210316131811/https://docs.ftx.com/#get-lending-history
//       - Historical lending rates: https://ftx.com/intl/spot-margin/lending (via Wayback Machine).
capture include build/code/get_lending_rates_ftx.ipynb

capture include build/code/get_lending_rates_bitfinex.ipynb

capture include build/code/convert_time_zones.ipynb

include build/code/clean_lending_rates.do

include build/code/clean_futures_funding.do

include build/code/clean_bbg_crypto.do

include build/code/clean_coingecko.do

include build/code/clean_iv_t3.do

capture include build/code/get_defillama_lending_data.ipynb

include build/code/clean_defillama.do

include build/code/clean_intraday_prices.do

include build/code/create_main_panel.do

******************************************************************************** 
* Run Analysis Code to make figures and tables; in order they appear
******************************************************************************** 

capture include analysis/code/figure_v.ipynb

include analysis/code/table_sumstats.do

include analysis/code/figure_funding_premia.do

include analysis/code/figure_funding_vs_lending.do

include analysis/code/regression_stablecoinRates_vs_y.do

include analysis/code/figure_balancesheet_vs_stablecoinRates.do

include analysis/code/redemption_sumstats.do

include analysis/code/regression_prop_peg_stability_first_stage.do

include analysis/code/regression_prop_peg_stability_second_stage.do

include analysis/code/regression_repeg.do

include analysis/code/figure_may_2022.do

include analysis/code/regression_mlb_iv.do

******************************************************************************** 
* Appendix
******************************************************************************** 

include analysis/code/figure_bbg_crypto_expected_returns.do

include analysis/code/figure_stablecoinRates_vs_er.do

include analysis/code/figure_prime_mmfs.do

include analysis/code/figure_market_cap_vs_funding_apr.do

include analysis/code/figure_tether_redemptions_and_issuance.do

include analysis/code/table_y_robustness.do

include analysis/code/regression_stablecoinRates_vs_er.do

include analysis/code/table_lending_rates_across_platforms.do

include analysis/code/table_correlation_effr.do

include analysis/code/regression_rho.do

include analysis/code/regression_r_btc_iv.do 


